package com.engine.salary.entity.salarysob.bo;

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salarysob.dto.SalarySobBasicFormDTO;
import com.engine.salary.entity.salarysob.param.SalarySobBasicSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.enums.SalaryCycleTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.util.SalaryI18nUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Date;
import java.util.Objects;

/**
 * 薪资账套
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/

public class SalarySobBO {

    /**
     * 薪资账套基础设置保存参数转换成薪资账套基础设置po
     *
     * @param saveParam  薪资账套基础设置保存参数
     * @param employeeId 人员id
     * @return
     */
    public static SalarySobPO convert2PO(SalarySobBasicSaveParam saveParam, Long employeeId) {
        Date now = new Date();
        if (saveParam == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
        }
        return SalarySobPO.builder()
                .name(saveParam.getName())
                .incomeCategory(saveParam.getTaxableItems())
                .salaryCycleType(saveParam.getSalaryCycleType())
                .salaryCycleFromDay(saveParam.getSalaryCycleFromDay())
                .taxCycleType(saveParam.getTaxCycleType())
                .attendCycleType(saveParam.getAttendCycleType())
                .attendCycleFromDay(saveParam.getAttendCycleFromDay())
                .socialSecurityCycleType(saveParam.getSocialSecurityCycleType())
                .disable(NumberUtils.INTEGER_ZERO)
                .creator(employeeId)
                .createTime(now)
                .updateTime(now)
                .deleteType(NumberUtils.INTEGER_ZERO)
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .taxAgentId(saveParam.getTaxAgentIds().get(0))
                .build();
    }

    /**
     * 解析薪资账套列表的薪资周期
     *
     * @param salarySobPO
     * @return
     */
    public static String buildSalaryCycle(SalarySobPO salarySobPO) {
        String salaryCycleStr;
        SalaryCycleTypeEnum salaryCycleTypeEnum = SalaryCycleTypeEnum.parseByValue(salarySobPO.getSalaryCycleType());
        if (salaryCycleTypeEnum == null) {
            return StringUtils.EMPTY;
        }
        Integer salaryCycleFromDay = salarySobPO.getSalaryCycleFromDay();
        switch (salaryCycleTypeEnum) {
            case BEFORE_LAST_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98395, "上上月1号——上上月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98399, "上上月{0}号——上月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            case LAST_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98396, "上月1号——上月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98400, "上月{0}号——本月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            case THIS_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98397, "本月1号——本月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98401, "本月{0}号——下月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            case NEXT_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98398, "下月1号——下月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98402, "下月{0}号——下下月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            default:
                salaryCycleStr = "";
        }
        return salaryCycleStr;
    }

    /**
     * 薪资账套po转换成薪资账套详情dto
     *
     * @param salarySobPO 薪资账套po
     * @return
     */
    public static SalarySobBasicFormDTO convert2FormDTO(SalarySobBasicFormDTO basicForm, SalarySobPO salarySobPO) {
        return basicForm
                .setId(salarySobPO.getId())
                .setName(salarySobPO.getName())
                .setName(salarySobPO.getName())
                .setTaxableItems(salarySobPO.getIncomeCategory())
                .setSalaryCycleType(salarySobPO.getSalaryCycleType())
                .setSalaryCycleFromDay(salarySobPO.getSalaryCycleFromDay())
                .setTaxCycleType(salarySobPO.getTaxCycleType())
                .setAttendCycleType(salarySobPO.getAttendCycleType())
                .setAttendCycleFromDay(salarySobPO.getAttendCycleFromDay())
                .setSocialSecurityCycleType(salarySobPO.getSocialSecurityCycleType())
                .setTaxAgentIds(salarySobPO.getTaxAgentIds())
                .setDescription(salarySobPO.getDescription());

    }
}
